
package hackathon.techbooster.tanabata;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.TextView;

public class VTextView extends TextView {
    // private static final int TOP_SPACE = 18;
    protected static final int TOP_SPACE = 0;

    // private static final int BOTTOM_SPACE = 18;
    protected static final int BOTTOM_SPACE = 0;

    protected static final int FONT_SIZE = 16;

    protected Typeface mFace;

    protected Paint mPaint;

    protected String text = "";

    protected int width;

    protected int height;

    protected float scale;

    public VTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        scale = context.getResources().getDisplayMetrics().density;
        mFace = Typeface.createFromFile(PreferenceUtils.getFontFile(context));
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setTextSize(FONT_SIZE * scale);
        mPaint.setColor(Color.BLACK);
        mPaint.setTypeface(mFace);
    }

    public void setText(String text) {
        this.text = text;
    }

    @Override
    public void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);

        width = getWidth();
        height = getHeight();
    }

    @Override
    public void onDraw(Canvas canvas) {
        float fontSpacing = mPaint.getFontSpacing();
        // float lineSpacing = fontSpacing * 2;
        float lineSpacing = fontSpacing * 1.5f; // 行間は0.5行分とする
        float x = width - lineSpacing;
        float y = TOP_SPACE + fontSpacing;
        // float y = TOP_SPACE + fontSpacing * 2;
        String[] s = text.split("");
        boolean newLine = false;

        for (int i = 1; i <= text.length(); i++) {
            newLine = false;
            fontSpacing = mPaint.getFontSpacing();

            if (s[i].equals("\n")) {
                newLine = true;
            } else if (CharSetting.checkSpecialData(s, i)) {
                // 特別チェックに通った場合はそのまま描画
                canvas.drawText(s[i], x, y, mPaint);
            } else {
                CharSetting setting = CharSetting.getSetting(s[i]);
                if (setting == null) {
                    // 文字設定がない場合、そのまま描画
                    canvas.drawText(s[i], x, y, mPaint);
                } else {
                    fontSpacing = mPaint.getFontSpacing() * setting.space;

                    // 文字設定が見つかったので、設定に従い描画
                    canvas.save();
                    canvas.rotate(setting.angle, x, y);
                    canvas.drawText(s[i], x + fontSpacing * setting.x, y + fontSpacing * setting.y,
                            mPaint);
                    canvas.restore();
                }

                if (y + fontSpacing > height - BOTTOM_SPACE && s.length > i + 1
                        && !CharSetting.isPunctuationMark(s[i + 1])) {
                    // もう文字が入らない場合
                    newLine = true;
                } else {
                    // まだ文字が入る場合
                    newLine = false;
                }
            }
            if (newLine) {
                // 改行処理
                x -= lineSpacing;
                y = TOP_SPACE + fontSpacing;
            } else {
                // 文字を送る
                y += fontSpacing;
            }
        }
    }
}
